import {createContext, useContext, Dispatch} from 'react'


export type initStateType = {
  pic: string
}

interface ContextType {
  state: initStateType | undefined,
  dispatch: Dispatch<{
    type: string,
    payload?: Partial<initStateType>
  }>
}

export const initState:initStateType = {
  pic: ""
}

export const reducer = (preState = initState, action: {
  type: string;
  payload?: Partial<initStateType>;
}) => {
  const {type} = action
  switch (type) {
    case "PIC":
      console.log(action!.payload!.pic)
      return {
        ...preState,
        pic: action!.payload!.pic
      }
    default:
      return preState
  }
}

export const ProductContext = createContext<ContextType>({
  state: initState,
  dispatch: () => {}
})


const useRedcuerContext = () => {
  return useContext(ProductContext);
}
export default useRedcuerContext
